package com.easyjava.builder.strategy.impl;

import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.builder.BuildMapperXml;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @program: workspace-easyjava
 * @description:
 * @author: cw
 * @create: 2024-05-29 15:34
 **/
public class CommonXmlImpl {

    public static String NAME_INSERT = "insert";
    public static String NAME_INSERT_OR_UPDATE = "insertOrUpdate";
    public static String NAME_INSERT_BATCH = "insertBatch";
    public static String NAME_INSERT_OR_UPDATE_BATCH = "insertOrUpdateBatch";


    public static void commonXml(TableInfo tableInfo, BufferedWriter bw, String name, FieldInfo field)
            throws IOException {
        String po = BuildMapperXml.getPo(tableInfo);

        bw.write("\t<insert id=\"" + name + "\" parameterType=\"" + po + "\">\n");
        if (field != null) {
            String fieldName = BuildMapperXml.INSERT_PREFIX + field.getFieldName();
            String javaType = field.getJavaType();
            bw.write("\t\t<selectKey keyProperty=\"" + fieldName + "\" resultType=\"" + javaType
                    + "\" order=\"AFTER\">\n");
            bw.write("\t\t\tSELECT LAST_INSERT_ID()\n");
            bw.write("\t\t</selectKey>\n");
        }

        // 字段名
        bw.write("\t\t<trim prefix=\"insert into " + tableInfo.getTableName()
                + " (\" suffix=\")\" suffixOverrides=\",\">\n");
        for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
            if (fieldInfo.getAutoIncrement() != null && fieldInfo.getAutoIncrement()) {
                continue;
            }
            String propertyName = BuildMapperXml.INSERT_PREFIX + fieldInfo.getPropertyName();
            String fieldName = fieldInfo.getFieldName();
            bw.write("\t\t\t<if test=\"" + propertyName + " != null\">\n");
            bw.write("\t\t\t\t" + fieldName + ",\n");
            bw.write("\t\t\t</if>\n");
        }
        bw.write("\t\t</trim>\n");

        // 字段值
        bw.write("\t\t<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\">\n");
        for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
            if (fieldInfo.getAutoIncrement() != null && fieldInfo.getAutoIncrement()) {
                continue;
            }
            String propertyName = BuildMapperXml.INSERT_PREFIX + fieldInfo.getPropertyName();
            bw.write("\t\t\t<if test=\"" + propertyName + " != null\">\n");
            bw.write("\t\t\t\t#{" + propertyName + "},\n");
            bw.write("\t\t\t</if>\n");
        }
        bw.write("\t\t</trim>\n");

        // 更新字段
        if (NAME_INSERT_OR_UPDATE.equals(name)) {
            Map<String, String> tempMap = new HashMap();
            for (Entry<String, List<FieldInfo>> entry : tableInfo.getKeyIndexMap().entrySet()) {
                List<FieldInfo> fieldInfos = entry.getValue();
                for (FieldInfo fieldInfo : fieldInfos) {
                    tempMap.put(fieldInfo.getFieldName(), fieldInfo.getFieldName());
                }
            }
            bw.write("\t\t<trim prefix=\"ON DUPLICATE KEY UPDATE\" suffix=\"\" suffixOverrides=\",\">\n");
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                if (tempMap.containsKey(fieldInfo.getFieldName())) {
                    continue;
                }
                String propertyName = BuildMapperXml.INSERT_PREFIX + fieldInfo.getPropertyName();
                String fieldName = fieldInfo.getFieldName();
                bw.write("\t\t\t<if test=\"" + propertyName + " != null\">\n");
                bw.write("\t\t\t\t" + fieldName + " = VALUES(" + fieldName + "),\n");
                bw.write("\t\t\t</if>\n");
            }
            bw.write("\t\t</trim>\n");
        }

        bw.write("\t</insert>\n\n");
    }


    public static void commonBatchXml(TableInfo tableInfo, BufferedWriter bw, String name) throws IOException {
        String po = BuildMapperXml.getPo(tableInfo);
        StringBuilder sb = new StringBuilder();
        StringBuilder sbItem = new StringBuilder();
        StringBuilder sbUpdate = new StringBuilder();
        for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
            if (fieldInfo.getAutoIncrement() != null && fieldInfo.getAutoIncrement()) {
                continue;
            }
            String fieldName = fieldInfo.getFieldName();
            sb.append(fieldName + ",");
            sbItem.append("#{item." + fieldInfo.getPropertyName() + "},");
            sbUpdate.append(fieldName + " = VALUES(" + fieldName + "),");
        }
        sb.deleteCharAt(sb.length() - 1);
        sbItem.deleteCharAt(sbItem.length() - 1);
        sbUpdate.deleteCharAt(sbUpdate.length() - 1);

        bw.write("\t<insert id=\"" + name + "\" parameterType=\"" + po + "\">\n");
        bw.write("\t\tinsert into " + tableInfo.getTableName() + "(" + sb + ") values \n");
        bw.write("\t\t<foreach collection=\"list\" item=\"item\" separator=\",\">\n");
        bw.write("\t\t\t(" + sbItem + ")\n");
        bw.write("\t\t</foreach>\n");

        if (NAME_INSERT_OR_UPDATE_BATCH.equals(name)) {
            bw.write("\t\tON DUPLICATE KEY UPDATE\n");
            bw.write("\t\t" + sbUpdate + "\n");
        }

        bw.write("\t</insert>\n\n");
    }
}
